
package com.jf.cloud.distribution.controller.app;

import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.distribution.model.DistributionUserWallet;
import com.jf.cloud.distribution.service.DistributionUserService;
import com.jf.cloud.distribution.service.DistributionUserWalletBillService;
import com.jf.cloud.distribution.service.DistributionUserWalletService;
import com.jf.cloud.distribution.vo.DistributionUserVO;
import com.jf.cloud.distribution.vo.DistributionUserWalletInfoVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 分销员钱包信息
 *
 * @author zz
 * @date 2021-08-09 14:14:10
 */
@RestController("appDistributionUserWalletController")
@RequestMapping("/distribution_user_wallet")
@Tag(name = "app-分销员钱包信息")
public class DistributionUserWalletController {

    @Autowired
    private DistributionUserWalletService distributionUserWalletService;

    @Autowired
    private DistributionUserService distributionUserService;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private DistributionUserWalletBillService distributionUserWalletBillService;

    @GetMapping
    @Operation(summary = "获取分销员钱包信息")
    public ServerResponseEntity<DistributionUserWalletInfoVO> getByWalletId() {
        //查看当前用户是否为分销员
        DistributionUserVO distributionUser = distributionUserService.getByUserId(AuthUserContext.get().getUserId());
        if (Objects.isNull(distributionUser)) {
            throw new LuckException("您还不是分销员");
        }
        DistributionUserWallet distributionUserWallet = distributionUserWalletService.getByDistributionUserId(distributionUser.getDistributionUserId());
        //计算积累收益
        Long haveWithdrawalSum = distributionUserWalletBillService.getHaveWithdrawalSum(distributionUserWallet.getWalletId());
        distributionUserWallet.setAccumulateAmount(haveWithdrawalSum+distributionUserWallet.getSettledAmount());
        DistributionUserWalletInfoVO userWalletInfoVO = mapperFacade.map(distributionUserWallet, DistributionUserWalletInfoVO.class);
        return ServerResponseEntity.success(userWalletInfoVO);
    }
}
